package com.szh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.Gson;
import com.szh.domain.OrderInfo;
import com.szh.domain.RefundInfo;
import com.szh.mapper.RefundInfoMapper;
import com.szh.service.OrderInfoService;
import com.szh.service.RefundInfoService;
import com.szh.utils.OrderNoUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class RefundInfoServiceImpl extends ServiceImpl<RefundInfoMapper, RefundInfo> implements RefundInfoService {

    private static final Logger LOGGER = LoggerFactory.getLogger(RefundInfoServiceImpl.class);

    @Autowired
    private OrderInfoService orderInfoService;

    /**
     * 根据订单号创建退款单
     * @param orderNo 订单号
     * @param reason 退款原因
     * @return 退款单
     */
    @Override
    public RefundInfo createRefundByOrderNo(String orderNo, String reason) {
        //根据订单号获取订单信息
        OrderInfo orderInfo = orderInfoService.getOrderByOrderNo(orderNo);
        //创建退款单
        RefundInfo refundInfo = new RefundInfo();
        refundInfo.setOrderNo(orderNo);
        refundInfo.setRefundNo(OrderNoUtils.getRefundNo());
        refundInfo.setTotalFee(orderInfo.getTotalFee());
        refundInfo.setRefund(orderInfo.getTotalFee());
        refundInfo.setReason(reason);
        this.save(refundInfo);
        return refundInfo;
    }

    /**
     * 更新退款单，微信支付
     * @param bodyAsString 响应体
     */
    @Override
    public void updateRefundForWxPay(String bodyAsString) {
        //将响应体的json字符串转换成Map
        Gson gson = new Gson();
        Map<String, String> resultMap = gson.fromJson(bodyAsString, Map.class);
        //根据退款单号修改退款单
        QueryWrapper<RefundInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("refund_no", resultMap.get("out_refund_no")); //商户退款单号
        RefundInfo refundInfo = new RefundInfo();
        refundInfo.setRefundId(resultMap.get("refund_id")); //微信支付退款单号
        //查询退款和申请退款中的返回参数
        if (null != resultMap.get("status")) {
            refundInfo.setRefundStatus(resultMap.get("status")); //退款状态
            refundInfo.setContentReturn(bodyAsString); //将全部响应结果存入数据库的content字段
        }
        //退款结果通知中的返回参数
        if (null != resultMap.get("refund_status")) {
            refundInfo.setRefundStatus(resultMap.get("refund_status")); //退款状态
            refundInfo.setContentNotify(bodyAsString); //将全部响应结果存入数据库的content字段
        }
        //更新退款单
        this.update(refundInfo, queryWrapper);
    }

    /**
     * 更新退款单
     */
    @Override
    public void updateRefundForAliPay(String refundNo, String content, String refundStatus) {
        LOGGER.info("更新退款单 ===> {}，退款状态更新为 ===> {}", refundNo, refundStatus);
        LambdaQueryWrapper<RefundInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RefundInfo::getRefundNo, refundNo);
        RefundInfo refundInfo = new RefundInfo();
        refundInfo.setRefundStatus(refundStatus); //退款状态
        refundInfo.setContentReturn(content); //将全部响应结果存入数据库的content字段
        this.update(refundInfo, queryWrapper);
    }
}
